package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.ProcessAlertDataDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.service.IAlertDataService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        //查询当前登陆人的userId
        Long userId = SecurityUtils.getUserId();
        if(userId==1){
            return alertDataMapper.selectAlertDataList(alertData);
        }
        alertData.setUserId(userId);
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 处理设备报警数据
     *
     * @param processAlertDataDto
     */
    @Override
    public void processingAlertData(ProcessAlertDataDto processAlertDataDto) {
        //获取当前登陆人的姓名和以及处理结果和时间
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        LocalDateTime processingTime = processAlertDataDto.getProcessingTime();
        String processingResult = processAlertDataDto.getProcessingResult();
        //根据报警记录Id查询当前报警信息
        Long alertDataId = processAlertDataDto.getId();
        LambdaQueryWrapper<AlertData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlertData::getId,alertDataId);
        AlertData alertData = getOne(queryWrapper);
        //将处理人的信息封装到alertData中并更新,并更新报警数据状态为1
        alertData.setUserId(userId);
        alertData.setProcessorName(username);
        alertData.setProcessingTime(processingTime);
        alertData.setProcessingResult(processingResult);
        alertData.setStatus(1);
        alertDataMapper.updateAlertData(alertData);

    }
}
